<template>
  <div class="app-container">
    <div class="card-box">
      <el-button type="primary" @click="handleToAdd">添加供应商</el-button>
    </div>
    <div class="card-box">
      <el-table
        v-loading="supplierApplyStore.loading.list || supplierApplyStore.loading.update"
        :data="supplierApplyStore.list"
        row-id="id"
      >
        <el-table-column label="ID" prop="id" />
        <el-table-column label="供应商名称" prop="name" />
        <el-table-column label="联系人" prop="contact_name" />
        <el-table-column label="联系电话" prop="contact_phone" />
        <el-table-column label="审核状态">
          <template #default="{ row }">
            <el-tag :type="row.status === 'approve' ? 'success' : 'danger'">
              {{ row.status === "approve" ? "通过" : "拒绝" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleToEdit(row)">编辑</el-button>
            <el-popconfirm title="是否继续删除？" @confirm="handleDelete(row)">
              <template #reference>
                <el-button type="danger" link>删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="params.page"
        v-model:page-size="params.size"
        style="margin-top: 10px"
        background
        :total="supplierApplyStore.totalCount"
        @change="supplierApplyStore.fetchList(params)"
      />
    </div>
    <Form
      v-model="visible"
      :type="formType"
      :form="currentForm"
      @confirm="supplierApplyStore.fetchList(params)"
    />
  </div>
</template>

<script setup lang="ts">
import {
  type SupplierApplyListQuery,
  type SupplierApplyForm,
  type SupplierApplyItemType,
} from "@/api/supplier/apply";
import { useSupplierApplyStoreHook } from "@/store/modules/supplier/apply";
import Form from "./components/form.vue";
import type { FormType } from "./components/form.vue";

const supplierApplyStore = useSupplierApplyStoreHook();

const visible = ref(false);

const formType = ref<FormType>("add");

const currentForm = ref<SupplierApplyForm>({
  is_active: false,
  name: "",
  contact_name: "",
  contact_phone: "",
  contact_email: "",
  status: "approve",
  business_license_url: "",
  id_card_1: "",
  id_card_2: "",
  reason: "",
  agent: "",
  user: "",
});

const params = reactive<SupplierApplyListQuery>({
  page: 1,
  size: 10,
});

function handleToAdd() {
  formType.value = "add";
  visible.value = true;
}

async function handleDelete(row: SupplierApplyItemType) {
  await supplierApplyStore.fetchDelete(row.id);
  await supplierApplyStore.fetchList(params);
}

function handleToEdit(row: SupplierApplyItemType) {
  console.log(row);
  formType.value = "edit";
  currentForm.value = {
    id: row.id,
    is_active: row.is_active,
    name: row.name,
    contact_name: row.contact_name,
    contact_phone: row.contact_phone,
    contact_email: row.contact_email,
    status: row.status,
    business_license_url: row.business_license_url,
    id_card_1: row.id_card_1,
    id_card_2: row.id_card_2,
    reason: row.reason,
    agent: row.agent ? row.agent.id : undefined,
    user: row.user ? row.user.id : "",
  };
  visible.value = true;
}

onMounted(() => {
  supplierApplyStore.fetchList(params);
});
</script>

<style scoped lang="scss"></style>
